package com.etc.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.etc.dao.OrderDetailDao;
import com.etc.dao.PersonDao;
import com.etc.dao.ProductInfoDao;
import com.etc.dao.impl.OrderDetailDaoImpl;
import com.etc.dao.impl.PersonDaoImpl;
import com.etc.dao.impl.ProductInfoDaoImpl;
import com.etc.entity.OrderInfo;
import com.etc.entity.Person;
import com.etc.entity.Product;
import com.etc.entity.ProductInfo;
import com.etc.service.OrderDetailService;
import com.etc.service.PersonService;
import com.etc.util.JdbcUtil;

public class OrderDetailServiceImpl implements OrderDetailService {
	OrderDetailDao oDetailDao = new OrderDetailDaoImpl();

	@Override
	public OrderInfo checkOut(int uid, List list) {
		// TODO Auto-generated method stub
		final String BALANCE_LACK = "1";
		final String OK = "2";//购买成功
		String msg = "";
		double total=0;
		String time="";//下单时间
		Map<String, String> map = null;
		int pid = 0;
		int num = 0;
		int oid=0;//订单id
		int stock=0;// 库存量
		Random r=new Random();
		PersonDao personDao = new PersonDaoImpl();
		Person person = personDao.findPersonById(uid);
		double balance = person.getBalance();// 用户余额
		ProductInfoDao productInfoDao = new ProductInfoDaoImpl();
		OrderDetailDao oDao=new OrderDetailDaoImpl();
		ProductInfo product=new ProductInfo();
		OrderInfo orderInfo=null;
		ArrayList<Product> productlist=new ArrayList<Product>();
		double price = 0;
		//计算购买的商品总价格
		for (int i = 0; i < list.size(); i++) {
			//遍历购买的每个商品
			map = (Map<String, String>) list.get(i);
			pid = Integer.parseInt(map.get("id").toString());//商品id
			num = Integer.parseInt(map.get("num").toString());//商品数量
			total+=productInfoDao.getPriceByNum(pid, num);
			}
		if(total>balance){//如果总价格大于余额，则购买失败
			return null;
		}
		Connection conn = JdbcUtil.getConn();
		try {
			conn.setAutoCommit(false);
			 balance=balance-total;
			// 更新数据库数据
			personDao.modifyBalance(conn, uid, balance);
			//大的订单 ： oid 总价total time ,uid
			boolean tag=true;//true 表示oid存在
			while(tag){
				//随机产生一个不重复的oid
				oid=r.nextInt(1000);
				tag=oDao.checkOid(conn,oid);
			}
			time=time();
			oDao.downOrder(conn, oid, uid, total,time);
			for(int i = 0; i < list.size(); i++){
			//遍历购买的每个商品
			map = (Map<String, String>) list.get(i);
			pid = Integer.parseInt(map.get("id"));//商品pid
			num = Integer.parseInt(map.get("num"));//商品数量
			product=productInfoDao.findPrductInfoById(conn,pid);
			stock=product.getStock()-num;
			productInfoDao.updateProductInfoById(conn, pid, stock);
			Product eachProduct=new Product(product.getName(), price, num);
			productlist.add(eachProduct);
			//插入 小订单 : oid,pid,quantity,total
			oDao.addEachProduct(conn, oid, pid, num, total);
			}
			//生成订单信息
			Person p1=personDao.findPersonById(uid);
			p1.setPwd("");
			orderInfo=new OrderInfo(oid, productlist, total, time, p1);
			conn.commit();
		} catch (Exception e) {
			try {
			conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			try {
				conn.setAutoCommit(true);
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return orderInfo;

	}
	/**
	 * 获取当前时间
	 * @return
	 */
		public String time() {
			Date date = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = format.format(date);
			return time;

		}

}
